package com.example.wangshuo.zyfypt_205ws.model;



import com.example.wangshuo.zyfypt_205ws.bean.TcaseBean;
import com.example.wangshuo.zyfypt_205ws.iface.TcaseListener;
import com.example.wangshuo.zyfypt_205ws.iface.Tcaseiface;
import com.example.wangshuo.zyfypt_205ws.service.TcaseService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wangshuo on 2017/4/29.
 */

public class TcaseModel implements Tcaseiface {
    private Retrofit retrofit;
    private String BASEURL = "http://amicool.neusoft.edu.cn/";
    //构造方法
    public TcaseModel(){
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }
    //获取文章数据
    @Override
    public void getResultList(String mod, int page, String sessionID, final TcaseListener listener) {
        TcaseService tcaseService = retrofit.create(TcaseService.class);
        Call<List<TcaseBean>> call = tcaseService.getResult(mod, page, sessionID);
        call.enqueue(new Callback<List<TcaseBean>>() {
            @Override
            public void onResponse(Call<List<TcaseBean>> call, Response<List<TcaseBean>> response) {
                if(response.isSuccessful() && response.body() != null){
                    listener.onResponse(response.body());
                } else {
                    listener.onFail("on response fail");
                }
            }
            @Override
            public void onFailure(Call<List<TcaseBean>> call, Throwable t) {

            }
        });
    }
}

